package com.fosss.stack;

public class ArrayStackDemo {
    public static void main(String[] args) {
        ArrayStack arrayStack = new ArrayStack(3);
        arrayStack.push(1);
        arrayStack.push(2);
        arrayStack.push(3);

        arrayStack.list();
        System.out.println("-----------------");
        System.out.println(arrayStack.pop());
    }
}

class ArrayStack{
    private int maxSize;//栈的大小
    private int[] stack;//数组模拟栈
    private int top=-1;//表示栈顶

    //构造器
    public ArrayStack(int maxSize){
        this.maxSize = maxSize;
        //将数组初始化！！！！！！！！！！！！！！！！！
        stack = new int[maxSize];
    }

    //栈满
    public boolean isFull(){
        return top==maxSize-1;
    }
    //栈空
    public boolean isEmpty(){
        return top==-1;
    }

    //入栈
    public void push(int value){
        //先判断栈是否满
        if(isFull()){
            throw new RuntimeException("栈满");
        }
        top++;
        stack[top]=value;
    }

    //出栈
    public int pop(){
        //判断是否为空
        if(isEmpty()){
            throw new RuntimeException("栈空");
        }
        int value=stack[top];
        top--;
        return value;
    }

    //遍历栈，从栈顶开始遍历
    public void list(){
        //判断是否栈空
        if(isEmpty()){
            throw new RuntimeException("栈空");
        }
        for(int i = top;i>=0;i--){
            System.out.printf("stack[%d]=%d\n",i,stack[i]);
        }
    }
}






















